package leetcode.editor.cn;

//Java：把二叉搜索树转换为累加树
public class W6cpku {
    public static void main(String[] args) {
        Solution solution = new W6cpku().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        int sum;

        public TreeNode convertBST(TreeNode root) {
            sum = 0;
            convertBST1(root);
            return root;
        }

        // 按右中左顺序遍历，累加即可
        public void convertBST1(TreeNode root) {
            if (root == null) {
                return;
            }
            convertBST1(root.right);
            sum += root.val;
            root.val = sum;
            convertBST1(root.left);
        }
    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}